﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using System.IO;
using Ext.Net;
using Ext.Net.MVC;
using WebEntities.Models.Framework;
using com.subb.WebFramework;

namespace WebUI.Areas.WebAdmin.Controllers
{
    [Authorize]
    public class SiteConfigController : Controller
    {
        private const string SITEARGS_PATH = "WebsiteArgs/";

        public ActionResult Index()
        {
            int argid = int.Parse(ConfigurationManager.AppSettings["SiteArgId"]);
            FW_SiteArgsDAL sdal = new FW_SiteArgsDAL();
            WebsiteConfig webcfg = WebsiteConfig.AsWebsiteConfig(sdal.GetWebConfig(argid));
            return View(webcfg);
        }

        [HttpPost]
        public ActionResult SaveConfig(WebsiteConfig webcfg)
        {
            FormPanelResult response = new FormPanelResult();
            response.IsUpload = true;
            string webpath = webcfg.BackgroundImage;
            try
            {
                FW_SiteArgsDAL sdal = new FW_SiteArgsDAL();
                int id = sdal.SaveWebConfig(webcfg);
                int cfgid = id > 0 ? id : webcfg.ArgID;
                //上传文件
                if (Request.Files.Count > 0)
                {
                    HttpFileCollectionBase fb = Request.Files;
                    HttpPostedFileBase fpfb1 = fb.Get("BackgroundImageFile");
                    string savePath = string.Empty;
                    //上传图片
                    if (fpfb1 != null && fpfb1.ContentLength > 0)
                    {
                        string uploadVFolder = ConfigurationManager.AppSettings["UploadVFolder"];
                        if (!string.IsNullOrWhiteSpace(uploadVFolder))
                        {
                            savePath = Server.MapPath(uploadVFolder + SITEARGS_PATH);
                            if (!Directory.Exists(savePath))
                            {
                                Directory.CreateDirectory(savePath);
                            }
                            string extName = Path.GetExtension(fpfb1.FileName);
                            savePath += "webackground1" + extName;
                            fpfb1.SaveAs(savePath);
                            //更新路径
                            webpath = uploadVFolder + SITEARGS_PATH + "webackground1" + extName;
                            WebsiteConfig tmpwebcfg = WebsiteConfig.AsWebsiteConfig(sdal.GetWebConfig(cfgid));
                            tmpwebcfg.BackgroundImage = webpath;
                            sdal.SaveWebConfig(tmpwebcfg);
                        }
                    }
                }
                response.Success = true;
                response.ExtraParams.Add(new Parameter("WebackPath", webpath));
                response.ExtraParams.Add(new Parameter("Msg", "参数配置已保存！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }

        [HttpPost]
        public ActionResult ClearWebackground(int ArgID)
        {
            FormPanelResult response = new FormPanelResult();
            response.IsUpload = true;
            try
            {
                WebsiteConfig webcfg = new WebsiteConfig();
                webcfg.ArgID = ArgID;
                FW_SiteArgsDAL sdal = new FW_SiteArgsDAL();
                sdal.SaveWebConfig(webcfg);
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "网页背景已清空！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }
    }
}
